<?php

        class IndexController{
                //载入文章数据库
                private $db;
                public function __construct(){
                        $articleData = include './Data/articleData.php';
                        $this->db=$articleData;

                }
                //首页
                public function Index(){
                        $data = $this->db;
                        krsort($data);
                        //p($data);
                        //用户列表
                        $userData = include './Data/data.php';
                        //p($userData);
                       include './Templates/Index.html';
                        //$this->display('Index',$data);
                }
                public function Add(){
                        if(!empty($_POST)){
                                if($_POST['title']=="" || $_POST['content']==""){
                                        msg('标题或者内容不能为空!','Index.php');
                                }
                                $_POST['addTime']= time();
                                $_POST['username']=$_SESSION['username'];
                                $this->db[]=$_POST;
                                //p($this->db);
                                $phpCode = "<?php return " .var_export($this->db,true)."?>";
                                if(file_put_contents('./Data/articleData.php',$phpCode)):
                                        msg("发表成功",'Index.php');
                                 endif;
                        }
                }
                //view
                public function View(){
                       $id = $_GET['id'];
                        $data =$this->db[$id];
                        $this->display('View',$data);
                }
                //加载模板方法
                protected function display($tpl,$data){
                        include "./Templates/{$tpl}.html";
                }
                //用户乱操作处理   __call函数必须传输两个参数    $func $params
                public function __call($func,$params){
                        msg("不存在{$func}方法",'Index.php');
                }
        }
?>